var i_page=1,page_size=10,i_order="",total_page=0;
$(function(){
	loadData();
})
function seachData(){
	i_page=1;
	page_size=10;
	loadData();
}

function loadData(){
	var key = $("#key").val();
	$.ajax({
		url: contextPath+'/console/log/logData',
		type: "post",
		timeout: 30000,
		dataType:"json",
		data:{
			pageNum:i_page,
			pageSize:page_size,
			key:key
		},
		success: function(data) {
			$("#log").html('')
			if(data){
				for(var i=0;i<data.root.length;i++){
					var item = data.root[i];
					var n = (i_page-1)*page_size+1+i;
					var html='<tr align="center"> ';
						html+='<td>'+n+'</td> ';
						html+='<td>'+item.remake+'</td> ';
						html+='<td align="left" style="width:500px;white-space:pre;text-overflow:ellipsis;overflow:hidden;">&nbsp;'+item.params+'</td> ';
						html+='<td>'+item.userName+'</td> ';
						html+='<td>'+item.ip+'</td> ';
						html+='<td>'+item.createTime+'</td> ';
						html+='<td><i class="icon ico-icosheach" onclick="javascript:detailView('+item.id+');"><span class="i-tip" style="left: -6.5px;">查看</span></i></td> ';
						html+='</tr> ';
						$("#log").append(html);
				}
				total_page = parseInt((parseInt(data.total)+page_size-1)/page_size);
				createPageHtml(data.total);
			}
		},error: function(){
			   layer.msg('网络异常，请重试');
		}
	});
}
function createPageHtml(total){
	if(total>0){
		var i=1;
		var html='<li>共'+total+'条数据</li>';
		html+='<li><a onclick="javascript:gotoPage(1)">首页</a></li>';
		html+='<li><a  onclick="javascript:prePage()">上一页</a></li>';
		
		if(i_page<5){
			for(var i=0;i<total_page;i++){
				html += '<li><a name="pageIndex_'+(i+1)+'">'+(i+1)+'</a></li>';
				if(i==4) break;
			}
			if(total_page>6){
				html += '<li>...</li>';
				html+='<li><a name="pageIndex_'+total_page+'">'+total_page+'</a></li>';
			}
		}else if(i_page>=5 && i_page<total_page-2){
			html+='<li><a name="pageIndex_1">'+1+'</a></li>';
			html += '<li>...</li>';
			html+='<li><a name="pageIndex_'+(i_page-2)+'">'+(i_page-2)+'</a></li>';
			html+='<li><a name="pageIndex_'+(i_page-1)+'">'+(i_page-1)+'</a></li>';
			html+='<li><a name="pageIndex_'+(i_page)+'">'+(i_page)+'</a></li>';
			html+='<li><a name="pageIndex_'+(parseInt(i_page)+1)+'">'+(parseInt(i_page)+1)+'</a></li>';
			html += '<li>...</li>';
			html+='<li><a name="pageIndex_'+total_page+'">'+total_page+'</a></li>';
		}else if(i_page>=total_page-2){
			html+='<li><a name="pageIndex_1">'+1+'</a></li>';
			html += '<li>...</li>';
			for(var i=total_page-4;i<=total_page;i++){
				html += '<li><a name="pageIndex_'+i+'">'+i+'</a></li>';
			}
		}
        
	    html+='<li><a onclick="javascript:nextPage()">下一页</a></li>';
	    html+='<li><a onclick="javascript:gotoPage('+total_page+')">尾页</a></li>'
	    $("#pageList").html(html);
	    $("a[name='pageIndex_"+i_page+"']").attr('class','cur');
	    $("a[name^='pageIndex']").click(function(){
	    	var t = $(this).text();
	    	gotoPage(t);
	    })
	}else{
		var html = '<tr><td colspan="7" align="center">没有查询到相应数据</td></tr>';
		$("#log").append(html);
	}
}
function prePage(){
	if(parseInt(i_page)-1>0){
		i_page=parseInt(i_page)-1;
	}
	loadData();
}
function nextPage(){
	if(parseInt(i_page)+1<=total_page){
		i_page=parseInt(i_page)+1;
	}
	loadData();
}
function gotoPage(p){
	if(p>0 && p<=total_page){
		i_page=p;
		loadData();
	}
}

function detailView(id){
	
	Ajax({
		url: contextPath+"/console/log/detailView/"+id,
		isMask : true,
		success: function (data) {
			if (data.success) {
				var content = '';
				content += '<div class="right-content">';
				content += '<div class="operate-nav">';
				content += '<div class="operate-contant">';
				content += '<table width="100%" border="0" cellspacing="0" cellpadding="0" >';
				content += '<tr>';
		    	content +='<td width="25%">用户ID:</td>';
		        content +='<td width="75%">'+data.data.userId+'</td>';
		        content +='</tr>';
		        
		        content +='<tr>';
		        content +='<td width="25%">用户名称:</td>';
		        content +='<td width="75%">'+data.data.userName+'</td>';
		        content +='</tr>';
		        content +='<tr>';
		        content +='<td>类名:</td>';
		        content +='<td>'+data.data.className+'</td>';
		        content +='</tr>';
		        content +='<tr>';
		        content +='<td>方法:</td>';
		        content +='<td>'+data.data.method+'</td>';
		        content +='</tr>';
		        content +='<tr>';
		        content +='<td>操作说明:</td>';
		        content +='<td>'+data.data.remake+'</td>';
		        content +='</tr>';
		        content +='<tr>';
		        content +='<td>入参:</td>';
		        content +='<td><textarea rows="15">'+JsonUti.convertToString(jQuery.parseJSON(data.data.params))+'</textarea></td>';
		        content +='</tr>';
		        content +='</table>';
		        content +='</div>';
		        content +='</div>';
		        content +='</div>';
			        
		        var winWidth = $(window).width();
		    	var winHeight = $(window).height();
			        apexLayer.OpenFrame({
						title:'日志详情',
						width:winWidth*0.8,
						height:winHeight*0.8,
						maxmin:true,
						closeBtn:1,
						scrollbar:false,
						content:content
						
					});
		
			} else {
				alert("日志加载失败");
			}
		}
	});
	
}

